/*
 *      traffic.cpp
 *
 *      Copyright 2007 Jan Tichavský <jenda@zenwalk>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */


#include <iostream>
#include <iomanip>
#include <unistd.h> //for usleep
#include <fstream>
#include <string>

using namespace std;


	ifstream myfile;

string parsenext(string line, int& pos, int& num) {
	string sub;
	
	pos = line.find_first_not_of ( " \t", pos );
	
	if (num == 0)
	{
		sub = line.substr( pos, line.find ( ":", pos ) - pos );
		pos = line.find ( ":", pos ) + 1;
	}
	else
	{
	
		if (num < 16)
		{
			sub = line.substr( pos, line.find_first_of ( " \t", pos ) - pos );
			pos = line.find_first_of ( " \t", pos );
		} else {
			sub = line.substr( pos ); //last piece: cut to end
		}
	
	}

	num++;
	return sub;
}
//01   5    10   15   20   25   30
//  eth0:2994907872 2638437    0

void end (void)
{
	cout << endl << "Thanks for using, bye" << endl;
	myfile.close();
	exit(0);
}


int main(int argc, char** argv)
{
	
	atexit (end);

	string line;
	string sub, ct;
	int pos = 0, num = 0;
	int fpos;
	int numline = 1;
	double bytes, total;
	time_t rawtime;
	
	myfile.open("/proc/net/dev"); //see the file: cat /proc/net/dev
	getline (myfile,line);
	getline (myfile,line);
	fpos = myfile.tellg(); //store current position (start of third line)
	getline (myfile,line); //loop device, fourth+ lines are real devices
	
	cout << "choose: ";
	
	while ( !myfile.eof() )
    {
		if (numline > 1)
		{
			cout << ", ";
		}
		
		cout << numline << " - " << parsenext(line, pos, num);
		
		pos = 0;
		num = 0;
		getline (myfile,line);
		numline++;
    }
    myfile.clear(); //clear the EOF flag to do seekg
	
	cout << " ? ";
	
	int pom;
	pom = getchar() - 48;
	
	//cout << pom << ", " << numline << ", " << char(numline+48) << endl;
	
	if ( !(pom > 0 && pom < numline) )
	{
		cout << "choice out of range";
		exit(0);
	}
	
	myfile.seekg(fpos);
	
	while (pom > 1)
	{
		getline (myfile,line);
		pom--;
	}
	
	fpos = myfile.tellg();
	
	//ruler for 80 char console align
	//cout << "1   5    10   15   20   25   30   35   40   45   50   55   60   65   70   75   E" << endl;
	
	for(;;) {
		
		//myfile.sync();
		num = 0;
		pos = 0;
		
		myfile.seekg(fpos); //back to start of choosen line
		getline (myfile,line);
		
		cout << '\r'; //return to start of current line
		cout << setfill(' ') << setw(9) << left << parsenext(line, pos, num) << ": " << flush; //device name, 5 chars length hardcoded now

		sub = parsenext(line, pos, num); //extract the number (downloaded bytes)
		bytes = strtod(sub.c_str(), NULL); //convert to double
		total = bytes;

		//cout << setfill(' ') << setw(12) << sub << " B, " << flush; //bytes
		cout << setfill('0') << setw(9) << setprecision(7) << left << (bytes/(1024*1024)) << " MB" << flush; //megabytes
		cout << " down, ";
		
		for (int i = 0; i < 7; i++)
		{
			parsenext(line, pos, num);
		}
		
		sub = parsenext(line, pos, num); //extract the number (downloaded bytes)
		bytes = strtod(sub.c_str(), NULL); //convert to double
		total += bytes;

		//cout << setfill(' ') << setw(12) << sub << " B, " << flush; //bytes
		cout << setfill('0') << setw(9) << setprecision(7) << left << (bytes/(1024*1024)) << " MB" << flush; //megabytes
		cout << " up, ";
		
		//cout << setfill(' ') << setw(12) << total << " B, " << flush; //bytes
		cout << setfill('0') << setw(9) << setprecision(7) << left << (total/(1024*1024)) << " MB" << flush; //megabytes
		cout << " tot ";
		
		time ( &rawtime ); //current time
		ct = ctime (&rawtime);
		cout << setfill(' ') << setw(16) << right << ct.substr(4, ct.length() - 1 - 4 - 5) << flush; //cut of last char
		
		usleep(100000); //0.1s sleeping
		
	}
	
	cout << endl;
	myfile.close();
	
	return 0;

}
